﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BFTest.Particles
{
    class CParticlesDGC : DrawableGameComponent
    {
        SpriteBatch particlesBatch;
        public SpriteBatch ParticlesBatch { get { return particlesBatch; } private set { particlesBatch = value; } }

        List<CParticle> particles = new List<CParticle>();
        public List<CParticle> Particles { get { return particles; } private set { particles = value; } }

        List<CParticleBlood> bloodParticles = new List<CParticleBlood>();
        List<CParticleSmoke> smokeParticles = new List<CParticleSmoke>();
        List<CParticleFire> fireParticles = new List<CParticleFire>();

        public CParticlesDGC(Game game)
            : base(game)
        {
            ParticlesBatch = new SpriteBatch(game.GraphicsDevice);
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            if (!BFTestGame.Battlefield.IsPaused)
            {
                for (int x = 0; x < Particles.Count; x++)
                {
                    if (Particles[x].Alive == false)
                    {
                        Remove(Particles[x]);
                        x--;        // Decrement because the list will shift up when removing the dead one.
                    }
                    else
                        Particles[x].Update(gameTime);
                }

                base.Update(gameTime);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            ParticlesBatch.Begin();
            if (BFTestGame.Battlefield.DebugShowInfo)
            {
                // Particle count
                ParticlesBatch.DrawString(BFTestGame.TooltipFont, "Particles: " + Particles.Count.ToString(), new Vector2(5, 15), Color.Black);
                ParticlesBatch.DrawString(BFTestGame.TooltipFont, "Blood: " + bloodParticles.Count.ToString(), new Vector2(5, 25), Color.Black);
                ParticlesBatch.DrawString(BFTestGame.TooltipFont, "Fire: " + fireParticles.Count.ToString(), new Vector2(5, 35), Color.Black);
                ParticlesBatch.DrawString(BFTestGame.TooltipFont, "Smoke: " + smokeParticles.Count.ToString(), new Vector2(5, 45), Color.Black);
            }
            ParticlesBatch.End();

            // Draw blood and smoke normally
            ParticlesBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
            
            foreach (CParticleBlood particle in bloodParticles)
                particle.Draw(ParticlesBatch);

            foreach (CParticleSmoke particle in smokeParticles)
                particle.Draw(ParticlesBatch);
            
            ParticlesBatch.End();

            // Draw fire particles with additive blending
            ParticlesBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);

            foreach (CParticleFire particle in fireParticles)
                particle.Draw(ParticlesBatch);
            
            ParticlesBatch.End();
        }

        public void Add(CParticle particle)
        {
            Particles.Add(particle);
            if (particle.GetType() == typeof(CParticleFire))
                fireParticles.Add((CParticleFire)particle);
            else if (particle.GetType() == typeof(CParticleSmoke))
                smokeParticles.Add((CParticleSmoke)particle);
            else if (particle.GetType() == typeof(CParticleBlood))
                bloodParticles.Add((CParticleBlood)particle);
        }

        public void Remove(CParticle particle)
        {
            Particles.Remove(particle);
            if (particle.GetType() == typeof(CParticleFire))
                fireParticles.Remove((CParticleFire)particle);
            else if (particle.GetType() == typeof(CParticleSmoke))
                smokeParticles.Remove((CParticleSmoke)particle);
            else if (particle.GetType() == typeof(CParticleBlood))
                bloodParticles.Remove((CParticleBlood)particle);
        }
    }
}
